package com.tsmti.core.util;/**
 * Created by ymc on 2018/7/4.
 */

import org.apache.commons.codec.binary.Base64;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;


/**
 * @Package：com.tsmti.apply.util
 * @Class：ReportGenerateUtil
 * @Description： TODO
 * @Author：ymc
 * @Date：Created in 2018/7/4 11:26
 * @Company:
 * @Version：
 * @Modified By:
 */

public class ReportGenerateUtil {
    private static Logger logger = LoggerFactory.getLogger(ReportGenerateUtil.class);
    private ReportGenerateUtil() {
    }


    public static synchronized String getReportHtml(VelocityContext velocityContext,String reportUrl) throws Exception {
        VelocityEngine engine = new VelocityEngine();
        engine.setProperty(Velocity.RESOURCE_LOADER, "class");
        engine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        engine.init();
        Template template = engine.getTemplate(reportUrl, "UTF-8");
        StringWriter writer = new StringWriter();
        template.merge(velocityContext, writer);
        return ConvertUtil.replaceUTF8Bom(writer.toString());
    }

    public static synchronized String getReportContent(VelocityContext velocityContext,String content) throws Exception {
        VelocityEngine engine = new VelocityEngine();
        engine.setProperty(Velocity.RESOURCE_LOADER, "class");
        engine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        engine.init();
        StringWriter writer = new StringWriter();
        engine.evaluate(velocityContext, writer, "", content);
        String returnHtml = ConvertUtil.replaceUTF8Bom(writer.toString());
        writer.close();
        return returnHtml;
    }

    public static String create128Code(String code) {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        try {
            Code128Bean bean = new Code128Bean();
            final int dpi = 150;
            // makes the narrow
            bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));
            bean.setHeight(8);
            bean.doQuietZone(true);
            // 两边空白区
            bean.setQuietZone(2);
            bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            bean.generateBarcode(canvas, code);
            canvas.finish();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("单号：{}生成条形码出错",code,e);
        }
        String base64Img= Base64.encodeBase64String(baos.toByteArray());
        return base64Img;
    }

    public static byte[] create128CodeImg(String code) {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        try {
            Code128Bean bean = new Code128Bean();
            final int dpi = 150;
            // makes the narrow
            bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));
            bean.setHeight(8);
            bean.doQuietZone(true);
            // 两边空白区
            bean.setQuietZone(2);
            bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            bean.generateBarcode(canvas, code);
            canvas.finish();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("单号：{}生成条形码出错",code,e);
        }
        return baos.toByteArray();
    }

}
